//
//  WHNSecondViewController.m
//  YiQiTingShu
//
//  Created by dllo on 15/11/5.
//  Copyright © 2015年 直到世界的尽头. All rights reserved.
//  榜单页,节目榜单三个推出页

#import "WHNSecondViewController.h"
#import "WHNTableViewCell.h"
#import "AFTools.h"
#import <MJExtension.h>
#import "WHNList.h"
#import <UIImageView+WebCache.h>
#import "AlbumFViewController.h"
#import "FPlayViewController.h"
#import "WHNFifthViewController.h"
#import <MJRefresh.h>
#import <MBProgressHUD.h>
#import "FitHeader.h"

@interface WHNSecondViewController ()<UITableViewDataSource, UITableViewDelegate>
@property(nonatomic, strong)UITableView *tableView;
@property(nonatomic, strong)NSMutableArray *dataSource;
@property(nonatomic, assign)NSInteger pageId;
@property(nonatomic, strong)MBProgressHUD *progressView;

@end

@implementation WHNSecondViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _dataSource = [NSMutableArray array];
    [self.view NightWithType:UIViewColorTypeNormal];
    [self.view addSubview:self.tableView];
    _pageId = 1;
    [self getData];
    [self reloadDataAndRefresh];
    [self.view addSubview:self.progressView];
    
//    [PlayerButton shareButton].hidden = YES;
    self.tabBarController.tabBar.hidden = YES;
    
}
//#pragma mark 视图将出现时
//-(void)viewWillDisappear:(BOOL)animated
//{
//  [PlayerButton shareButton].hidden = NO;
//}

#pragma mark 获取数据
-(void)getData
{
    NSString *string = [NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/discovery/v1/rankingList/%@?device=android&key=%@&pageId=%ld&pageSize=40", _typeString, _keyString, _pageId];
    [AFTools getDataByURLString:string WithDataBlock:^(id data) {
        //MJExtension
        //数组到数组对象
        [_dataSource addObjectsFromArray:[WHNList mj_objectArrayWithKeyValuesArray:[data objectForKey:@"list"]]];
        [_tableView reloadData];
        [_tableView.mj_header endRefreshing];
        [_tableView.mj_footer endRefreshing];
        [_progressView removeFromSuperview];
    }];
}
#pragma mark 刷新加载
-(void)reloadDataAndRefresh
{
    if ([_typeString isEqualToString:@"anchor"]) {
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            [_dataSource removeAllObjects];
            _pageId = 1;
            [self getData];
        }];
    }
    else {
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            [_dataSource removeAllObjects];
            _pageId = 1;
            [self getData];
        }];
        _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            _pageId ++;
            [self getData];
        }];
    }
}
-(MBProgressHUD *)progressView
{
    if (!_progressView) {
        _progressView = [[MBProgressHUD alloc]initWithView:self.view];
        _progressView.labelText = @"加载中";
        [_progressView setMode:MBProgressHUDModeIndeterminate];
        [_progressView show:YES];
        [_progressView NightWithType:UIViewColorTypeNormal];
    }
    return _progressView;
}

#pragma mark 懒加载tableview初始化
-(UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, ScreenHeight - 64) style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tableView;
}
#pragma mark tableview cell样式
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if ([_typeString isEqualToString:@"track"]) {
        static NSString *indetifier = @"track";
        WHNTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:indetifier];
        if (!cell) {
            cell = [[WHNTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indetifier];
        }
        cell.kind = WHNTableViewCellKindOne;
        if (_dataSource.count != 0) {
            cell.NumberLabel.text = [NSString stringWithFormat:@"%ld", indexPath.row + 1];
            [cell.MainImageView sd_setImageWithURL:[NSURL URLWithString:[_dataSource[indexPath.row]coverSmall]] placeholderImage:[UIImage imageNamed:@"placeholderZh"]];
            cell.TitleLabel.text = [_dataSource[indexPath.row]title];
            cell.NameLabel.text = [NSString stringWithFormat:@"by %@", [_dataSource[indexPath.row]nickname]];
        }
        return cell;
    }
    else if ([_typeString isEqualToString:@"album"]) {
        static NSString *indetifier = @"album";
        WHNTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:indetifier];
        if (!cell) {
            cell = [[WHNTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indetifier];
        }
        cell.kind = WHNTableViewCellKindTwo;
        if (_dataSource.count != 0) {
            cell.NumberLabel.text = [NSString stringWithFormat:@"%ld", indexPath.row + 1];
            [cell.MainImageView sd_setImageWithURL:[NSURL URLWithString:[_dataSource[indexPath.row]coverMiddle]] placeholderImage:[UIImage imageNamed:@"placeholderZh"]];
            cell.TitleLabel.text = [_dataSource[indexPath.row]title];
            cell.NameLabel.text = [_dataSource[indexPath.row]intro];
            cell.JiLabel.text = [NSString stringWithFormat:@"%@集", [_dataSource[indexPath.row]tracksCounts]];
            cell.IconImageView.image = [UIImage imageNamed:@"ji"];
            cell.LeftImageView.image = [UIImage imageNamed:@"xiangyou"];
        }
        return cell;
    }
    else {
        static NSString *indetifier = @"cell";
        WHNTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:indetifier];
        if (!cell) {
            cell = [[WHNTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indetifier];
        }

        cell.kind = WHNTableViewCellKindTwo;
        if (_dataSource.count != 0) {
            cell.NumberLabel.text = [NSString stringWithFormat:@"%ld", indexPath.row + 1];
            [cell.MainImageView sd_setImageWithURL:[NSURL URLWithString:[_dataSource[indexPath.row]middleLogo]] placeholderImage:[UIImage imageNamed:@"placeholderZh"]];
            cell.TitleLabel.text = [_dataSource[indexPath.row]nickname];
            cell.NameLabel.text = [_dataSource[indexPath.row]personDescribe];
            CGFloat a = [[_dataSource[indexPath.row] followersCounts]integerValue] / 10000 * 1.0;
            if (a >= 1.0){
                cell.JiLabel.text = [NSString stringWithFormat:@"%.1f万", a];
            }
            else {
                cell.JiLabel.text = [NSString stringWithFormat:@"%@万", [_dataSource[indexPath.row] followersCounts]];
            }
            cell.IconImageView.image = [UIImage imageNamed:@"yonghu"];
            cell.LeftImageView.image = [UIImage imageNamed:@"xiangyou"];

        }
        return cell;
    }
    return 0;
}
#pragma mark cell个数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (_dataSource.count == 0) {
        return 0;
    }
    return _dataSource.count;
}
#pragma mark cell的高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100 * FitHeight;
}
#pragma mark 点击方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([_typeString isEqualToString:@"track"]) {
        FPlayViewController *playVC = [[FPlayViewController alloc]init];
        playVC.trackID = [_dataSource[indexPath.row] trackId];
        playVC.trackUid = [_dataSource[indexPath.row] uid];
        playVC.albumId = [_dataSource[indexPath.row] albumId];
        [self.navigationController pushViewController:playVC animated:YES];
    }
    else if ([_typeString isEqualToString:@"album"]) {
        AlbumFViewController *albumVC = [[AlbumFViewController alloc]init];
        albumVC.trackID = [_dataSource[indexPath.row] trackId];
        albumVC.alumbTitel = [_dataSource[indexPath.row] title];
        albumVC.albumId = [_dataSource[indexPath.row] albumId];
        [self.navigationController pushViewController:albumVC animated:YES];
    }
    else {
        WHNFifthViewController *fifthVC = [[WHNFifthViewController alloc]init];
        fifthVC.uidString = [_dataSource[indexPath.row] uid];
        [self.navigationController pushViewController:fifthVC animated:YES];
    }
}











- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
